@section('title', '产品列表')
@section('header')
<blockquote class="layui-elem-quote">列表中已经过滤该产品及已选中的相关产品，本页面暂不支持跨页选择，选择后先点确认。</blockquote>

<div class="layui-inline">
    <select id="class_id" name="class_id" class="layui-select-sm" lay-ignore>
        {$classList}
    </select>
</div>
<div class="layui-inline">
    <select id="search_type" name="search_type" class="layui-select-sm" lay-ignore>
        <option value="">搜索方式</option>
        <option value="title">标题</option>
        <option value="intro">描述</option>
    </select>
</div>
<div class="layui-inline">
    <input type="text" name="search_text" value="{$input['search_text'] ?? ''}" id="search_text" class=" layui-input layui-input-sm" style="width: 120px;" />
</div>
<div class="layui-inline">
    <button class="layui-btn layui-btn-sm layui-btn-normal" type="button" id="btn_search" value="搜索">搜索</button>
    <button type="button"  class="layui-btn layui-btn-sm layui-btn-normal" onclick="resetForm()" value="重置">重置</button>
    <button class="layui-btn layui-btn-sm layui-btn-danger btnChoose">关联所选产品</button>
</div>

@endsection
@section('table')
<style type="text/css">
    .layui-textarea{ min-height: 60px;}
</style>
<input type="hidden" value="{$input['ids'] ?? ''}" id="related_ids" name="related_ids">
<input type="hidden" value="{$input['id'] ?? ''}" id="id" name="id">

{if(empty($list))}
<div class="no_data">
    没有符合条件的记录
</div>
{else}
<table class="layui-table" lay-even lay-skin="nob" style="margin-bottom: 15px;">
    <colgroup>
        <col width="50">
        <col class="hidden-xs" width="50">
        <col class="hidden-xs" width="260">
        <col class="hidden-xs" width="100">
        <col class="hidden-xs" width="60">
        <col class="hidden-xs" width="60">
        <col class="hidden-xs" width="180">
    </colgroup>
    <thead>
    <tr>
        <th><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
        <th class="hidden-xs">ID</th>
        <th class="hidden-xs">标题</th>
        <th>状态</th>
        <th>置顶</th>
        <th>推荐</th>
        <th>创建时间</th>
    </tr>
    </thead>
    <tbody>
    {foreach $list as $v}
    <tr>
        <td><input type="checkbox" name="ids[]" data-title="{$v.title}" data-url="{{content_url($v['id'], $v['class_id'])}}" value="{$v['id']}" {if in_array($v['id'], $ids)}checked{/if} lay-skin="primary" /></td>
        <td class="hidden-xs">{$v['id']}</td>
        <td class="hidden-xs"><a href="{{content_url($v['id'], $v['class_id'])}}" target="_blank">{$v['title']}</a></td>
        <th class="hidden-xs"><input type="checkbox" name="state" value="{$v['id']}" {$v['state'] == 1 ?'checked':''} lay-skin="switch" lay-filter="switchState" lay-text="上架|下架" title="状态"></th>
        <th class="hidden-xs"><input type="checkbox" name="is_top" value="{$v['id']}" {$v['is_top'] == 1 ?'checked':''} lay-skin="switch" lay-filter="switchState" lay-text="是|否" title="置顶"></th>
        <th class="hidden-xs"><input type="checkbox" name="is_recommend" value="{$v['id']}" {$v['is_recommend'] == 1 ?'checked':''} lay-skin="switch" lay-filter="switchState" lay-text="是|否" title="推荐"></th>

        <td class="hidden-xs">{$v['created_at']}</td>

    </tr>
    {/foreach}
    </tbody>
</table>
{/if}
{if(! empty($list))}
<div style="margin-bottom: 15px;">
    <div class="layui-inline">
        <button class="layui-btn layui-btn-sm layui-btn-danger btnChoose">确定关联所选产品</button>
    </div>

</div>
{/if}

{$pageBar}

@endsection
@section('js')
<script>
    layui.use(['form', 'jquery','laydate', 'layer'], function() {
        var form = layui.form,
            $ = layui.jquery,
            laydate = layui.laydate,
            layer = layui.layer
        ;
        $('form.layui-form').attr('action', '{{url('index')}}');
        $('#search_type').val("{$input['search_type'] ?? ''}");
        $('#class_id').val("{$input['class_id'] ?? 0}");

        $('#btn_search').click(function(){
            var args = $('#search_form').serialize();
            var baseUrl = '{{url('popup')}}';
            args += '&ids='+$('#related_ids').val()+'&id='+$('#id').val();
            if(baseUrl.indexOf('?') == -1){
                baseUrl += '?'+args;
            }else{
                baseUrl += '&'+args;
            }
            window.location.href = baseUrl;
        })


        form.render();
    });

    //处理选中的文章，这里可以选择多条
    $('button.btnChoose').click(function(){
        var chk_value =[];
        let data_list = [];
        if($('input[name="ids[]"]:checked').size() == 0){
            layer.msg('请选择要关联的记录',{shift: 6,icon:5});
            return false;
        }
        $('input[name="ids[]"]:checked').each(function(){
            chk_value.push($(this).val());
            let id = $(this).val();
            let title = $(this).attr('data-title');
            let url = $(this).attr('data-url');
            data_list.push({id:id,title:title,url:url});
        });
        closePopup();
        parent.saveRelatedInfo(data_list);
    });


    function closePopup(){
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }

</script>
@endsection
@extends('admin.common.list')